package de.gameduell.framework.asset {
	import de.gameduell.framework.asset.Asset;
//	import de.gameduell.framework.debug.Debug;
	
	import sandy.core.scenegraph.Group;
	import sandy.parser.IParser;
	import sandy.parser.Parser;
	import sandy.parser.ParserEvent;		

	/**
	 * @author cbra
	 */
	public class ModelAsset extends Asset {
		/**
		 * Parameter that is used to specify that the ASE (ASCII Scene Export)
		 */
		public static const ASE:String = "ASE";
		/**
		 * Parameter that is used to specify that the 3DS (3D Studio) Parser 
		 */
		public static const MAX_3DS:String = "3DS";
		/**
		 * Parameter that is used to specify that the COLLADA (COLLAborative 
		 */
		public static const COLLADA:String = "DAE";
		
		
		private var parser:IParser;
		private var _percentLoaded:int = 0;
		private var _group:Group;
		
		public function ModelAsset(aId:String, aUrl:String, format:String) {
			super(aId, aUrl);
			parser = Parser.create(aUrl, format);	  
			parser.addEventListener(ParserEvent.FAIL, parserError);
			parser.addEventListener(ParserEvent.INIT, modelComplete);
			parser.addEventListener(ParserEvent.PROGRESS, modelProgress);	
		}
		
		override public function load():void{
			parser.parse();
		}
		
		override public function get percentLoaded():int{
			return _percentLoaded;
		}
		
		public function get group():Group{
			return _group;
		}
		
		protected function modelProgress(event:ParserEvent):void {
			_percentLoaded = event.percent;
			progress(event);
		}
		
		protected function modelComplete(event:ParserEvent):void {
			_group = event.group;
			complete(event);
		}
		
		private function parserError(event:ParserEvent):void{
			var errorEvent:AssetEvent = new AssetEvent(AssetEvent.LOAD_ERROR,this);
			_error = "ModelAsset "+_id+" has created a parser error";
			dispatchEvent(errorEvent);
		}


	}
}
